define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluedu/user/index' + location.search,
                    add_url: '',
                    edit_url: 'xiluedu/user/edit',
                    del_url: 'xiluedu/user/del',
                    multi_url: 'xiluedu/user/multi',
                    course_url: 'xiluedu/user/course',
                    moneylog_url: 'xiluedu/user/moneylog',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'vip.vip_id', title: __('VIP等级'), searchList: Config.vipConfig, formatter: Table.api.formatter.flag },
                        { field: 'username', title: __('Username'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'email', title: __('Email'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'gender', title: __('Gender'), visible: false, searchList: { 1: __('Male'), 0: __('Female') } },
                        { field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true },
                        { field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true },
                        { field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true },
                        { field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        { field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search },
                        { field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), buttons: [
                                {
                                    name: 'course',
                                    title: __('他的课程'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-file-video-o',
                                    url: function (row) {
                                        return Fast.api.fixurl($.fn.bootstrapTable.defaults.extend.course_url + '?user_id=' + row.id)
                                    },
                                },
                                
                                {
                                    name: 'moneylog',
                                    title: __('收入日志'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-book',
                                    url: function (row) {
                                        return Fast.api.fixurl($.fn.bootstrapTable.defaults.extend.moneylog_url + '?user_id=' + row.id)
                                    },
                                },
                                {
                                    name: 'VIP',
                                    title: __('设置VIP'),
                                    text: 'VIP',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'xiluedu/user/setvip',//接口地址 控制器名/方法名
                                    // url: function (row) {
                                    //     return Fast.api.fixurl($.fn.bootstrapTable.defaults.extend.setvip_url + '?user_id=' + row.id)
                                    // },
                                }
                            ], table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        course: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluedu/user/course',
                    table: 'user_course',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'user_id', title: __('用户id'), visible: false },
                        { field: 'course2.name', title: __('课程名称'), operate: "LIKE" },
                        { field: 'createtime', title: __('时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        moneylog: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluedu/user/moneylog',
                    table: 'user_money_log',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'user_id', title: __('用户id'), visible: false },
                        { field: 'money', title: __('变更佣金'), operate: false },
                        // {field: 'before', title: __('变更前总佣金'), operate: false},
                        { field: 'after', title: __('变更后总佣金'), operate: false },
                        { field: 'memo', title: __('描述') },
                        { field: 'createtime', title: __('添加时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        setvip: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
